/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.cjb.malacma.rssfeed.entity;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

import static java.lang.System.*;

/**
 *
 * @author malacma
 *
 */
@NamedQueries({
    @NamedQuery(name = "Tag.findByTag", query = "SELECT t FROM Tag t WHERE t.txt=:txt"),
    @NamedQuery(name = "Tag.findAll", query = "SELECT t FROM Tag t")
})
@Entity
public class Tag implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE)
    private Long id;
    
    @Column(nullable = false,length=150, unique = true, updatable = false)
    private String txt;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTxt() {
        return txt.toUpperCase();
    }

    public void setTxt(String txt) {
        this.txt = txt.toUpperCase();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Tag other = (Tag) obj;
        if ((this.txt == null) ? (other.txt != null) : !this.txt.equals(other.txt)) {
            return false;
        }
        return true;
    }

    @PrePersist
    private void prePersist() throws Exception {
        if (this.txt == null || this.txt.equals("")) {
            throw new Exception("A tag não pode ser NULL!");
        }

        if (this.txt.length() > 150) {
            this.txt = this.txt.substring(150);
        }
        this.txt = this.txt.toUpperCase();
    }

    @PreUpdate
    private void preUpdate()  throws Exception{
        if (this.txt == null || this.txt.equals("")) {
            throw new Exception("Erro ao atualizar a TAG");
        }
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 59 * hash + (this.txt != null ? this.txt.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return txt;
    }


}
